/*
 * @Descripttion: 
 * @version: 
 * @Author: sueRimn
 * @Date: 2022-10-10 09:40:58
 * @LastEditors: sueRimn
 * @LastEditTime: 2022-11-29 16:08:39
 */
/*
 * @Descripttion: 
 * @version: 
 * @Author: sueRimn
 * @Date: 2022-09-30 14:29:50
 * @LastEditors: sueRimn
 * @LastEditTime: 2022-10-28 14:49:07
 */
let util = require("../util");
// let operate = require("../db/opeate")
// let db = require("../db/index");
let { query } = require("../db/pool")
exports.add = (req, res) => {
  let params = req.body;
  params.id = util.uuidGenerator();
  params.component_element = params.component_code + "Element";
  let strSql = "insert into data_view(id,code,component_code,description,sort_number,create_author,last_modified_author,content,component_element) values('" + params.id + "' ,'" + params.code + "' ,'" + params.component_code + "' ,'" + params.description + "' ,'" + params.sort_number + "' ,'" + params.create_author + "' ,'" + params.create_author + "' ,'" + params.content + "' ,'" + params.component_element + "');";
  dataViewQuery(strSql, res);
}
exports.getList = (req, res) => {
  let params = req.body;
  let wh = 'where ';
  for (let key in params) {
    (!util.isEmpty(params[key])) ? wh += key + "='" + params[key] + "' and " : "";
  }
  wh = wh.substring(0, wh.lastIndexOf("and")) + ";";
  let strSql = "select * from data_view ";
  wh.trim() != "where" ? strSql += wh : strSql += ";";
  dataViewQuery(strSql, res);
}
exports.edit = (req, res) => {
  let params = req.body;
  params.component_element = params.component_code + "Element";
  let strSql = "update data_view set code='" + params.code + "' ,description='" + params.description + "' ,sort_number='" + params.sort_number + "' ,last_modified_author='" + params.last_modified_author + "' ,component_code='" + params.component_code + "' ,content='" + params.content + "' ,component_element='" + params.component_element + "' where id='" + params.id + "';";
  // operate.update(strSql, res);
  dataViewQuery(strSql, res);
}
exports.delete = (req, res) => {
  let params = req.query;
  let strSql = "delete from data_view where id='" + params.id + "';";
  // operate.delete(strSql, res);
  dataViewQuery(strSql, res);
}

async function dataViewQuery(sql, res) {
  let rtn = await query(sql);
  res.send(rtn);
}